﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Common;
using NanoERP.Business.BaseInfo;

namespace NanoERP.BaseInfo
{
    //[Module("FrmProductInfo")]
    public partial class FrmProductInfo : ModuleForm
    {
        Goodsinfo _goodsInfo;
        readonly GoodsinfoBLL _goodsInfoBll;
        readonly SysdictableBLL _sysdictablebLl;
        readonly Sysdictable _sysdictable;
        public FrmProductInfo()
        {
            InitializeComponent();
        }

        public FrmProductInfo(Sysdictable v)
        {
            InitializeComponent();

            _sysdictablebLl = new SysdictableBLL();
            _goodsInfoBll = new GoodsinfoBLL();
            _goodsInfo = new Goodsinfo();

            _sysdictable = v;

            FillFormInfo();
            BindFormContro();
        }

        public FrmProductInfo(Goodsinfo goodsInfo, Sysdictable v)
        {
            InitializeComponent();

            _sysdictablebLl = new SysdictableBLL();
            _goodsInfoBll = new GoodsinfoBLL();
            _goodsInfo = goodsInfo;
            _sysdictable = v;

            FillFormInfo();
            BindFormContro();
        }

        void FillFormInfo()
        {
            cboGoodsUnit.DataSource = _sysdictablebLl.Query((int)SysDicEnum.基本单位);
            cboGoodsModel.DataSource = _sysdictablebLl.Query((int)SysDicEnum.型号);
            cboGoodsColor.DataSource = _sysdictablebLl.Query((int)SysDicEnum.颜色);
            cboGoodsSpec.DataSource = _sysdictablebLl.Query((int)SysDicEnum.规格);
            cboOtherUnit.DataSource = _sysdictablebLl.Query((int)SysDicEnum.其他单位);
            cboPriceCosting.DataSource = _sysdictablebLl.Query((int)SysDicEnum.成本核算方法);
        }

        /// <summary>
        /// 绑定控件值
        /// </summary>
        void BindFormContro()
        {
            if (_goodsInfo.GoodsDirectory != null)
            {
                var obj = _sysdictablebLl.Get(_goodsInfo.GoodsDirectory.Value);
                txtGoodsDirectory.Text = obj == null ? "" : obj.Dicname;
            }

            txtGoodsName.DataBindings.Add(new Binding("Text", _goodsInfo, "GoodsName", true, DataSourceUpdateMode.OnPropertyChanged));
            txtGoodsPym.DataBindings.Add(new Binding("Text", _goodsInfo, "GoodsPym", true, DataSourceUpdateMode.OnPropertyChanged));
            txtGoodsNo.DataBindings.Add(new Binding("Text", _goodsInfo, "GoodsNo", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPackingCoef.DataBindings.Add(new Binding("Text", _goodsInfo, "PackingCoef", true, DataSourceUpdateMode.OnPropertyChanged));
            txtBarcode.DataBindings.Add(new Binding("Text", _goodsInfo, "Barcode", true, DataSourceUpdateMode.OnPropertyChanged));
            txtGoodsType.DataBindings.Add(new Binding("Tag", _goodsInfo, "GoodsType", true, DataSourceUpdateMode.OnPropertyChanged));
            cboGoodsUnit.DataBindings.Add(new Binding("Text", _goodsInfo, "GoodsUnit", true, DataSourceUpdateMode.OnPropertyChanged));
            txtGoodsDirectory.DataBindings.Add(new Binding("Tag", _goodsInfo, "GoodsDirectory", true, DataSourceUpdateMode.OnPropertyChanged));
            cboGoodsModel.DataBindings.Add(new Binding("SelectedValue", _goodsInfo, "GoodsModel", true, DataSourceUpdateMode.OnPropertyChanged));
            cboGoodsColor.DataBindings.Add(new Binding("Text", _goodsInfo, "GoodsColor", true, DataSourceUpdateMode.OnPropertyChanged));
            cboGoodsSpec.DataBindings.Add(new Binding("Text", _goodsInfo, "GoodsSpec", true, DataSourceUpdateMode.OnPropertyChanged));
            diGoodsWeight.DataBindings.Add(new Binding("ValueObject", _goodsInfo, "GoodsWeight", true, DataSourceUpdateMode.OnPropertyChanged));
            diGoodsVolume.DataBindings.Add(new Binding("ValueObject", _goodsInfo, "GoodsVolume", true, DataSourceUpdateMode.OnPropertyChanged));
            cboOtherUnit.DataBindings.Add(new Binding("Text", _goodsInfo, "OtherUnit", true, DataSourceUpdateMode.OnPropertyChanged));
            txtRatio.DataBindings.Add(new Binding("Text", _goodsInfo, "Ratio", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPriceRetail.DataBindings.Add(new Binding("Text", _goodsInfo, "PriceRetail", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPriceWholesale.DataBindings.Add(new Binding("Text", _goodsInfo, "PriceWholesale", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPricePre1.DataBindings.Add(new Binding("Text", _goodsInfo, "PricePre1", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPricePre2.DataBindings.Add(new Binding("Text", _goodsInfo, "PricePre2", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPriceLow.DataBindings.Add(new Binding("Text", _goodsInfo, "PriceLow", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPriceHigh.DataBindings.Add(new Binding("Text", _goodsInfo, "PriceHigh", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPriceIn1.DataBindings.Add(new Binding("Text", _goodsInfo, "PriceIn1", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPriceIn2.DataBindings.Add(new Binding("Text", _goodsInfo, "PriceIn2", true, DataSourceUpdateMode.OnPropertyChanged));
            txtNote.DataBindings.Add(new Binding("Text", _goodsInfo, "Note", true, DataSourceUpdateMode.OnPropertyChanged));
            cboPriceCosting.DataBindings.Add(new Binding("SelectedValue", _goodsInfo, "PriceCosting", true, DataSourceUpdateMode.OnPropertyChanged));

            chIsValid.Checked = _goodsInfo.IsValid.Value.Equals(1);
            _goodsInfo.GoodsType = _sysdictable.Id;
            txtGoodsType.Text = _sysdictable.Dicname;
            txtGoodsType.Tag = _sysdictable.Id;
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            _goodsInfo = new Goodsinfo();
            ControlUtil.ClearControlBind(groupPanel1);
            BindFormContro();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            if (!SaveInfo()) return;
            this.DialogResult = DialogResult.OK;
        }

        private bool SaveInfo()
        {
            if (string.IsNullOrWhiteSpace(txtGoodsName.Text))
            {
                txtGoodsName.Focus();
                MessageBox.Show("商品名称不能为空，请输入.", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }

            _goodsInfo.IsValid = chIsValid.Checked ? (short)1 : (short)0;
            _goodsInfoBll.Edit(_goodsInfo);

            MessageBox.Show("保存成功.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return true;
        }

        private void tsbSaveAdd_Click(object sender, EventArgs e)
        {
            if (!SaveInfo()) return;
            _goodsInfo = new Goodsinfo();
            ControlUtil.ClearControlBind(groupPanel1);
            BindFormContro();
        }

        private void tsmPre_Click(object sender, EventArgs e)
        {
            PreviewRecord(_goodsInfo.Id - 1, 0);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            PreviewRecord(_goodsInfo.Id + 1, 1);
        }

        /// <summary>
        /// 浏览记录 上一条 下一条
        /// </summary>
        /// <param name="id">浏览记录Id</param>
        /// <param name="type">类型 0-向上,1-向下</param>
        private void PreviewRecord(long id, int type)
        {
            bool isLast = false;
            var obj = _goodsInfoBll.PreRecord(id, type, ref isLast);
            _goodsInfo = obj;
            ControlUtil.ClearControlBind(groupPanel1);
            BindFormContro();

            if (isLast) MessageBox.Show(string.Format("当前已是{0}一条记录", type.Equals(0) ? "第" : "最后"));
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnGoodsType_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(txtGoodsType, SysDicEnum.商品信息);
            _goodsInfo.GoodsType = Normal.ToInt(txtGoodsType.Tag);
        }

        private void btnUnit_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboGoodsUnit, SysDicEnum.基本单位);
        }

        private void btnDirectory_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(txtGoodsDirectory, SysDicEnum.所属目录);
            _goodsInfo.GoodsDirectory = Normal.ToInt(txtGoodsDirectory.Tag);
        }

        private void btnModel_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboGoodsModel, SysDicEnum.型号);
            _goodsInfo.GoodsModel = Normal.ToInt(cboGoodsModel.Tag);
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboGoodsColor, SysDicEnum.颜色);
        }

        private void btnSpec_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboGoodsSpec, SysDicEnum.规格);
        }

        private void btnOtherUnit_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboOtherUnit, SysDicEnum.其他单位);
        }

        private void txtGoodsName_Leave(object sender, EventArgs e)
        {
            txtGoodsPym.Text = ChineseToSpell.GetChineseSpell(txtGoodsName.Text);
        }
    }
}
